Skip to content

Method: SingularPendingReference(Object, Field)

1: /**
2: * Copyright (C) 2022 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jsonld.deserialization.reference;
16:
17: import cz.cvut.kbss.jsonld.common.BeanClassProcessor;
18: import cz.cvut.kbss.jsonld.exception.TargetTypeException;
19:
20: import java.lang.reflect.Field;
21: import java.util.Objects;
22:
23: /**
24: * Represents a singular pending reference.
25: * <p>
26: * That is, a singular attribute referencing an object.
27: */
28: public final class SingularPendingReference implements PendingReference {
29:
30: private final Object targetObject;
31:
32: private final Field targetField;
33:
34: public SingularPendingReference(Object targetObject, Field targetField) {
35: this.targetObject = Objects.requireNonNull(targetObject);
36: this.targetField = Objects.requireNonNull(targetField);
37: }
38:
39: @Override
40: public void apply(Object referencedObject) {
41: assert referencedObject != null;
42: if (!targetField.getType().isAssignableFrom(referencedObject.getClass())) {
43: throw new TargetTypeException(
44: "Cannot assign referenced object " + referencedObject + " of type " + referencedObject
45: .getClass() + " to field " + targetField);
46: }
47: BeanClassProcessor.setFieldValue(targetField, targetObject, referencedObject);
48: }
49:
50: @Override
51: public boolean equals(Object o) {
52: if (this == o) {
53: return true;
54: }
55: if (o == null || getClass() != o.getClass()) {
56: return false;
57: }
58: SingularPendingReference that = (SingularPendingReference) o;
59: return targetObject.equals(that.targetObject) && targetField.equals(that.targetField);
60: }
61:
62: @Override
63: public int hashCode() {
64: return Objects.hash(targetObject, targetField);
65: }
66: }